<template>
  <modal
    title="新建角色"
    v-model="showModal"
    width="720"
  >
    <Form ref="formName" :model="formData" :rules="formRules">
      <FormItem label="角色名" prop="role">
        <Input v-model="formData.role" placeholder="请输入角色名"/>
      </FormItem>
      <FormItem label="角色描述" prop="descript">
        <Input v-model="formData.descript" id="descript" placeholder="请输入角色描述"/>
      </FormItem>

    </Form>

    <div class="demo-drawer-footer">
      <Button style="margin-right: 8px"  :loading="modal_loading" @click="cancel">取消</Button>
      <Button type="primary"  :loading="modal_loading" @click="save">保存</Button>
    </div>
  </modal>
</template>

<script>
  import {saveRole} from '@/api/base-manage/role'

  export default {
    name: 'role-add',
    props: {
      reloadData:{
        type:Function,
        default:null
      },
      para:{},
    },
    data () {
      return {
        showModal: false,
        formData:{
          id: '',
          role:'',
          descript:''
        },
        formRules: {},
        modal_loading: false,
      }
    },
    mounted () {
    },
    methods: {
      show () {
        this.showModal = true
      },
      cancel () {
        this.showModal = false;
      },
      save(){
        this.modal_loading = true;
        let params = {
          roleData: this.formData
        };
        saveRole(params).then(res=>{
          if(res.data.success){
            this.modal_loading = false;
            this.showModal = false;
            this.$Message.success(res.data.msg||'保存成功');
            this.reloadData();
          }else{
            this.modal_loading = false;
            this.$Message.error(res.data.msg||'保存失败');
          }
        }).catch(err=>{
          console.log(err);
          this.$Message.error('连接错误');
          this.modal_loading = false;
        })
      },
      reset(){
        let newObj = {};
        _.forEach(this.formData, function (v, k) {
          newObj[k] = ''
        });
        this.formData = newObj;
      },
      initData (obj) {
        let me = this;
        obj = obj || '';
        _.forEach(Object.keys(me.formData),function (v,i) {
            me.formData[v] = obj[v];
        })
      },
    },
    watch:{
      showModal(){
        this.modal_loading=false
      }
    }
  }
</script>

<style scoped>

</style>
